/**
* 作者 ：食草狂魔
*
* 日期 ：2017/11/28
*
* 描述 ：系统管理组件 - 编辑部门
*/
<template>
  <div class="dialogContent">
    <el-form class="addDepartmentForm" :rules="rules" label-position="right" label-width="110px" :model="ruleForm" ref="addDepartmentForm">
      <el-row class="dialogBar">
        <el-col class="title">部门管理</el-col>
      </el-row>
      <el-form-item label="部门名称 :" prop="name" style="margin-top: 20px;">
        <p>{{ruleForm.name}}</p>
      </el-form-item>
      <el-form-item label="部门编码 :" prop="code" style="margin-top: 20px;">
        <p>{{ruleForm.code}}</p>
      </el-form-item>
      <el-form-item label="部门描述 :" prop="description">
        <p>{{ruleForm.description}}</p>
      </el-form-item>
      <el-form-item label="类别 :" prop="type" style="margin-top: 20px;">
        <p v-if="ruleForm.type==1">部门</p>
        <p v-else-if="ruleForm.type==2">单位</p>
        <p v-else-if="ruleForm.type==3">组织</p>
        <p v-else>部门</p>
      </el-form-item>
      <el-form-item label="上级部门 :" prop="parentId" style="margin-top: 20px;">
        <p>{{ ruleForm.parentName }}</p>
      </el-form-item>
      <el-form-item label-width="0px" style="margin-top: 20px;text-align: center;">
        <!--<el-button class="moduleButton button" :plain="true" type="primary" @click="submitForm('ruleForm')">保存</el-button>-->
        <el-button class="button" type="info" @click="resetForm('ruleForm')">关闭</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import departmentAPIs from '../../../../api/departmentAPIs'
  export default {
    props: ["parentModel", "updateDepartmentformData"],
    name: "addDepartmentForm",
    data () {
      return {
        ruleForm: {
          sysDepartmentId: "",
          name: "",
          code: "",
          description: "",
          type: "",
          parentId: "",
          token: localStorage.token
        },
        rules:{
        	name: [
            { required: true, message: '请填写内容', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '请填写内容', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '请填写内容', trigger: 'blur' }
          ]
        },
        options: [{
          value: '1',
          label: '部门'
        }, {
          value: '2',
          label: '单位'
        }, {
          value: '3',
          label: '组织'
        }],
        departmentOptions: [],
        departmentGroup: []
      }
    },
    mounted: function () {
      let vm = this

      console.log(vm.$props.updateDepartmentformData)

      vm.ruleForm.sysDepartmentId = vm.$props.updateDepartmentformData.sysDepartmentId
      vm.ruleForm.name = vm.$props.updateDepartmentformData.sysDepartmentName
      vm.ruleForm.code = vm.$props.updateDepartmentformData.sysDepartmentCode
      vm.ruleForm.description = vm.$props.updateDepartmentformData.description
      vm.ruleForm.type = vm.$props.updateDepartmentformData.type
      vm.ruleForm.parentName = vm.$props.updateDepartmentformData.parentName || "无"
//      vm.ruleForm.parentId = vm.$props.updateDepartmentformData.resources ? vm.$props.updateDepartmentformData.resources.split(",") : []
      vm.departmentGroup = ["101", "101001", "101001002", "101001002002"]

      departmentAPIs.listTree({
        token: localStorage.token
      }).then(({ data }) => {
        if(data.code == 10000) {
          vm.departmentOptions = data.data
        }
      })
    },
    methods: {
      submitForm(formData) {
        let vm = this
        /*vm.$refs.addDepartmentForm.validate((valid) => {
          if (valid) {*/
						vm.ruleForm.parentId = vm.departmentGroup.length>1 ? vm.departmentGroup.pop() : ""
		        departmentAPIs.insert(vm.ruleForm).then(({data}) => {
		          if(data.code==10000){
		            vm.$message.success("添加部门成功！");
		            vm.$refs.addDepartmentForm.resetFields()
		            vm.$parent.visible = false

		            vm.$props.parentModel.tableOptions.pageInfo.currentPage = 1
		            vm.$props.parentModel.loadData(vm.$props.parentModel.tableOptions.pageInfo.currentPage, vm.$props.parentModel.tableOptions.pageInfo.pageSize)
		          }
		        })
          /*} else {
            return false;
          }
        });*/

      },
      resetForm() {
        /*vm.$refs.addDepartmentForm.resetFields()*/
        this.$parent.visible = false
      }
    }
  }
</script>
<style>
  .dialogContent .dialogBar{
    height: 35px;
    line-height: 35px;
    background-color: #ebebeb;
    border-left: 4px solid #2cace2;
    text-align: left;
    width: 97%;
    margin: 0 auto;
  }
  .dialogContent .dialogBar .title{
    margin-left: 10px;
    color: #2cace2;
    font-size: 1.8rem;
  }
  .dialogContent .shortText{
    width: 45%;
  }
  .dialogContent .longText {
    width: 96%;
  }
  .addDepartmentForm .button{
    width: 112px;
    height: 36px;
    padding: 0;
  }
  .addDepartmentForm button:first-child{
    margin-right: 60px;
  }
</style>

